<?php
include('lib/ftree_utils.php');
require_once "HTML/Template/IT.php";

function embedGraphic($fn)
{
	global $tpl;

	$dir = "media/img";
	$path = "$dir/$fn";
	$html = "<img src=\"$path\" alt=\"picture\">\n";
	$tpl->setVariable("MediaItem", $html);
}

function embedAudio($fn)
{
	global $tpl;

	$dir = "media/audio";
	$path = "$dir/$fn";
	//$html = "<a href=\"$path\">Audio File</a>\n";
	$html = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
		. "width=\"200\" height=\"16\" "
		. 'CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab"> '
		. "<PARAM name=\"SRC\" VALUE=\"$path\"> "
		. '<PARAM name="AUTOPLAY" VALUE="false"> '
		. '<PARAM name="CONTROLLER" VALUE="true"> '
		. "<embed src=\"$path\" autostart=\"false\" width=\"200\" height=\"16\" pluginspage=\"http://www.apple.com/quicktime/download/\"><noembed><a href=\"$path\">Audio File</a></noembed></embed> "
		. '</OBJECT>';
	$tpl->setVariable("MediaItem", $html);
}

function markGraphic($fn)
{
	global $tpl;

        $tpl->setCurrentBlock("Graphic") ;
	$dir = "media/img";
	$path = "$dir/$fn";
	$tpl->setVariable("MediaItemLink", $path);

	$tpl->setVariable("ActivateGraphic", "");
	$tpl->parseCurrentBlock("Graphic") ;
}

function markAudio($fn)
{
	global $tpl;

        $tpl->setCurrentBlock("Audio") ;
	$dir = "media/audio";
	$path = "$dir/$fn";
	$tpl->setVariable("MediaItemLink", $path);

	$tpl->setVariable("ActivateAudio", "");
	$tpl->parseCurrentBlock("Audio") ;
}




// GET HTTP "GET" VARS

if ( isset($_GET['id']) ) {
        $id = $_GET['id'];
} else {
        $id = 3054;
}
$id=preg_replace("/[^0-9]/","",$id);  //get rid of anything but numbers
if ($id < 1 || $id > pow(2,17)) {
        die("ID is out of range.");
}

// SET UP TEMPLATE

$isMobile = ChkIsMobile();
$tpl = new HTML_Template_IT("templates");
$isMobile ? $template = "person_media_mtmpl.html" : $template = "person_media_tmpl.html";
$status = $tpl->loadTemplatefile($template, true, true);

/* Connecting, selecting database */
$link = mysql_connect("localhost", "ftreeweb", "ftree_web")
   or die("Could not connect : " . mysql_error());
mysql_select_db("ftree") or die("Could not select database");


// GET PERSON

$query = "SELECT * FROM person WHERE `id` = $id";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$idData = mysql_fetch_array($result, MYSQL_ASSOC);

$full = formatName($idData['first'], $idData['middle'], $idData['last'], $idData['suffix'], $idData['nick'], "fnmls");

$tpl->setVariable("PersonID", $id);
$tpl->setVariable("FullName", $full);

// GET MEDIA

$query = "SELECT * FROM media WHERE `link_table` = 'person' AND `link_id` = $id ORDER BY `id`";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

while ($media = mysql_fetch_array($result, MYSQL_ASSOC)) {
	switch ($media['type']) {
	case "img":
		markGraphic($media['path']);
		break;
	case "audio":
		markAudio($media['path']);
		break;
	}
        $tpl->setCurrentBlock("Media") ;
        $tpl->setVariable("Title", $media['title']) ;
        $tpl->setVariable("Descr", $media['descr']) ;
        $tpl->parseCurrentBlock("Media") ;
}

mysql_close($link);

$tpl->show();
?>
